#include "debuginformationdlg.h"
#include "ui_debuginformationdlg.h"

DebugInformationDlg::DebugInformationDlg(int styleId,QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DebugInformationDlg)
{
    ui->setupUi(this);
    move(0,0);
    initialDisplayStyle(styleId);
//#ifdef BUILD_ARM
//    setGeometry(70,128,460,595);
//#endif

    timer=new QTimer();
    connect( timer, SIGNAL(timeout()), this, SLOT(timeOutSlot()) );


    model_progremEngine = new QStandardItemModel();
    QStringList labels = QObject::trUtf8("变量,value,-").simplified().split(",");
    model_progremEngine->setHorizontalHeaderLabels(labels);
    ui->tableView_programEngine->setColumnWidth(1,120);

    model_motionEngine = new QStandardItemModel();
    QStringList labels2 = QObject::trUtf8("变量,value,-").simplified().split(",");
    model_motionEngine->setHorizontalHeaderLabels(labels2);
    ui->tableView_motionEngine->setColumnWidth(1,120);

    model_taskInfo = new QStandardItemModel();
    QStringList labels3 = QObject::trUtf8("变量,value,-").simplified().split(",");
    model_taskInfo->setHorizontalHeaderLabels(labels3);
    ui->tableView_taskInfo->setColumnWidth(1,120);
}

void DebugInformationDlg::initialDisplayStyle(int styleId)
{
    switch (styleId) {
    case 1:
        this->setGeometry(0,0,600,450);
        ui->tabWidget->setGeometry(10,10,550,380);

        ui->tableView_taskInfo->setGeometry(10,20,420,320);
        ui->tableView_programEngine->setGeometry(10,30,420,310);
        ui->tableView_motionEngine->setGeometry(10,40,420,300);

        ui->checkBox_autoRefresh->setGeometry(20,400,160,40);
        ui->pushButton_hide->setGeometry(430,400,140,40);
        break;
    default:
        break;
    }
}

void DebugInformationDlg::showDebugInfo_taskInfo(TaskManagerInfo taskInfo)
{
    model_taskInfo->clear();
    //定义item
    QStandardItem* item = 0;

    int lineSize=10;

    for(int i=0;i<taskInfo.runningLine.size();i++)
    {
        item = new QStandardItem("任务号");
        model_taskInfo->setItem(lineSize*i+0,0,item);
        item = new QStandardItem(QString::number(taskInfo.taskIndex[i]));
        model_taskInfo->setItem(lineSize*i+0,1,item);

        item = new QStandardItem("fileName");
        model_taskInfo->setItem(lineSize*i+1,0,item);
        item = new QStandardItem(QString("%1").arg(
                                     QString::fromStdString(taskInfo.fileName[i])));
        model_taskInfo->setItem(lineSize*i+1,1,item);

        item = new QStandardItem("runningLine");
        model_taskInfo->setItem(lineSize*i+2,0,item);
        item = new QStandardItem(QString::number(taskInfo.runningLine[i],'f',3));
        model_taskInfo->setItem(lineSize*i+2,1,item);

        item = new QStandardItem("runningThreadId");
        model_taskInfo->setItem(lineSize*i+3,0,item);
        QString tmpStr;
        tmpStr.sprintf("%x",taskInfo.runningThreadId[i]);
        item = new QStandardItem(tmpStr);
        model_taskInfo->setItem(lineSize*i+3,1,item);

        item = new QStandardItem("taskThreadIsRunning");
        model_taskInfo->setItem(lineSize*i+4,0,item);
        item = new QStandardItem(QString("%1").arg(taskInfo.taskThreadIsRunning[i]));
        model_taskInfo->setItem(lineSize*i+4,1,item);

        item = new QStandardItem("taskThreadIsFinished");
        model_taskInfo->setItem(lineSize*i+5,0,item);
        item = new QStandardItem(QString("%1").arg(taskInfo.taskThreadIsFinished[i]));
        model_taskInfo->setItem(lineSize*i+5,1,item);

        item = new QStandardItem("isRunState");
        model_taskInfo->setItem(lineSize*i+6,0,item);
        item = new QStandardItem(QString("%1").arg(taskInfo.state[i]));
        model_taskInfo->setItem(lineSize*i+6,1,item);

        item = new QStandardItem("isInLoop");
        model_taskInfo->setItem(lineSize*i+7,0,item);
        item = new QStandardItem(QString("%1").arg(taskInfo.isInLoop[i]));
        model_taskInfo->setItem(lineSize*i+7,1,item);


    }
    ui->tableView_taskInfo->setModel(model_taskInfo);
    ui->tableView_taskInfo->resizeColumnToContents(0);
    ui->tableView_taskInfo->resizeColumnToContents(1);
    ui->tableView_taskInfo->resizeColumnToContents(2);
    ui->tableView_taskInfo->show();
}


DebugInformationDlg::~DebugInformationDlg()
{
    delete ui;
}

void DebugInformationDlg::on_pushButton_hide_clicked()
{
    hide();
}

void DebugInformationDlg::on_checkBox_autoRefresh_clicked(bool checked)
{
    if(true==checked)
    {
       timer->start( 500 );
    }
    else
    {
        timer->stop();
    }
}

void DebugInformationDlg::timeOutSlot()
{
    emit getPogramEngineDebugInfoSignal();
    emit getMotionEngineDebugInfoSignal();
    emit getTaskInfoSignal();
}

void DebugInformationDlg::showDebugInfo_programEngine(std::vector<ProgramEngineDebugInfo> infoList)
{
    model_progremEngine->clear();
    //定义item
    QStandardItem* item = 0;

    int lineSize=10;

    for(int i=0;i<infoList.size();i++)
    {
        item = new QStandardItem("任务号");
        model_progremEngine->setItem(lineSize*i+0,0,item);
        item = new QStandardItem(QString("%1######").arg(i));
        model_progremEngine->setItem(lineSize*i+0,1,item);

        item = new QStandardItem("progId_current");
        model_progremEngine->setItem(lineSize*i+1,0,item);
        item = new QStandardItem(QString("%1").arg(infoList[i].programIndex_currentRunning));
        model_progremEngine->setItem(lineSize*i+1,1,item);

        item = new QStandardItem("progCountNumber");
        model_progremEngine->setItem(lineSize*i+2,0,item);
        item = new QStandardItem(QString("%1").arg(infoList[i].programCountNum));
        model_progremEngine->setItem(lineSize*i+2,1,item);

        item = new QStandardItem("runningLine");
        model_progremEngine->setItem(lineSize*i+3,0,item);
        item = new QStandardItem(QString::number(infoList[i].runningLineFloat,'f',3));
        model_progremEngine->setItem(lineSize*i+3,1,item);

        item = new QStandardItem("progInstrucType");
        model_progremEngine->setItem(lineSize*i+4,0,item);
        item = new QStandardItem(QString("%1").arg(infoList[i].programInstructionType));
        model_progremEngine->setItem(lineSize*i+4,1,item);

        item = new QStandardItem("progEngineStatus");
        model_progremEngine->setItem(lineSize*i+5,0,item);
        item = new QStandardItem(QString("%1").arg(infoList[i].engineStatus));
        model_progremEngine->setItem(lineSize*i+5,1,item);

        item = new QStandardItem("motionStage");
        model_progremEngine->setItem(lineSize*i+6,0,item);
        item = new QStandardItem(QString("%1").arg(infoList[i].motionStage));
        model_progremEngine->setItem(lineSize*i+6,1,item);

        item = new QStandardItem("motionInsAdd");
        model_progremEngine->setItem(lineSize*i+7,0,item);
        item = new QStandardItem(QString("%1").arg(infoList[i].motionInstrutionAdd));
        model_progremEngine->setItem(lineSize*i+7,1,item);

        item = new QStandardItem("PorgEngine_STAGE");
        model_progremEngine->setItem(lineSize*i+8,0,item);
        item = new QStandardItem(QString("%1").arg(infoList[i].stage));
        model_progremEngine->setItem(lineSize*i+8,1,item);
    }
    ui->tableView_programEngine->setModel(model_progremEngine);
    ui->tableView_programEngine->resizeColumnToContents(0);
    ui->tableView_programEngine->resizeColumnToContents(1);
    ui->tableView_programEngine->resizeColumnToContents(2);
    ui->tableView_programEngine->show();

}


void DebugInformationDlg::showDebugInfo_programEngine(ProgramEngineDebugInfo info)
{
    model_progremEngine->clear();
    //定义item
    QStandardItem* item = 0;

    item = new QStandardItem("PE_STAGE");
    model_progremEngine->setItem(0,0,item);
    item = new QStandardItem(QString("%1").arg(info.stage));
    model_progremEngine->setItem(0,1,item);

    item = new QStandardItem("progId_cr");
    model_progremEngine->setItem(1,0,item);
    item = new QStandardItem(QString("%1").arg(info.programIndex_currentRunning));
    model_progremEngine->setItem(1,1,item);

    item = new QStandardItem("progCountN");
    model_progremEngine->setItem(2,0,item);
    item = new QStandardItem(QString("%1").arg(info.programCountNum));
    model_progremEngine->setItem(2,1,item);

    item = new QStandardItem("runningLine");
    model_progremEngine->setItem(3,0,item);
    item = new QStandardItem(QString::number(info.runningLineFloat,'f',3));
    model_progremEngine->setItem(3,1,item);

    item = new QStandardItem("progInsType");
    model_progremEngine->setItem(4,0,item);
    item = new QStandardItem(QString("%1").arg(info.programInstructionType));
    model_progremEngine->setItem(4,1,item);

    item = new QStandardItem("engineStatus");
    model_progremEngine->setItem(5,0,item);
    item = new QStandardItem(QString("%1").arg(info.engineStatus));
    model_progremEngine->setItem(5,1,item);

    item = new QStandardItem("motionStage");
    model_progremEngine->setItem(6,0,item);
    item = new QStandardItem(QString("%1").arg(info.motionStage));
    model_progremEngine->setItem(6,1,item);

    item = new QStandardItem("motionInsAdd");
    model_progremEngine->setItem(7,0,item);
    item = new QStandardItem(QString("%1").arg(info.motionInstrutionAdd));
    model_progremEngine->setItem(7,1,item);

    ui->tableView_programEngine->setModel(model_progremEngine);
    ui->tableView_programEngine->resizeColumnToContents(0);
    ui->tableView_programEngine->resizeColumnToContents(1);
    ui->tableView_programEngine->resizeColumnToContents(2);
    ui->tableView_programEngine->show();

}



void DebugInformationDlg::showDebugInfo_motionServer(MotionDebugInfo info)
{
    model_motionEngine->clear();
    //定义item
    QStandardItem* item = 0;

    item = new QStandardItem("deltaTime");
    model_motionEngine->setItem(0,0,item);
    item = new QStandardItem(QString("%1").arg(info.deltaTime));
    model_motionEngine->setItem(0,1,item);

    item = new QStandardItem("ClRemainTime");
    model_motionEngine->setItem(1,0,item);
    item = new QStandardItem(QString::number(info.currentLineRemainingTime,'f',3));
    model_motionEngine->setItem(1,1,item);

    item = new QStandardItem("currentLine");
    model_motionEngine->setItem(2,0,item);
    item = new QStandardItem(QString::number(info.currentLine,'f',3));
    model_motionEngine->setItem(2,1,item);

    item = new QStandardItem("lineQueueSize");
    model_motionEngine->setItem(3,0,item);
    item = new QStandardItem(QString("%1").arg(info.lineQueueSize));
    model_motionEngine->setItem(3,1,item);

    item = new QStandardItem("ServerMode");
    model_motionEngine->setItem(4,0,item);
    item = new QStandardItem(QString("%1").arg(info.motionServerMode));
    model_motionEngine->setItem(4,1,item);

    item = new QStandardItem("Status");
    model_motionEngine->setItem(5,0,item);
    item = new QStandardItem(QString("%1").arg(info.basicStatus));
    model_motionEngine->setItem(5,1,item);

    item = new QStandardItem("commandQuSize");
    model_motionEngine->setItem(6,0,item);
    item = new QStandardItem(QString("%1").arg(info.commandQueueSize));
    model_motionEngine->setItem(6,1,item);

    item = new QStandardItem("isVelpEmpty");
    model_motionEngine->setItem(7,0,item);
    item = new QStandardItem(QString("%1").arg(info.isVelprofileEmpty));
    model_motionEngine->setItem(7,1,item);

    item = new QStandardItem("timeAxisScale0");
    model_motionEngine->setItem(8,0,item);
    item = new QStandardItem(QString::number(info.timeAxisScale0,'f',7));
    model_motionEngine->setItem(8,1,item);

    item = new QStandardItem("comThrdStage");
    model_motionEngine->setItem(9,0,item);
    item = new QStandardItem(QString("%1").arg(info.commonThreadStage));
    model_motionEngine->setItem(9,1,item);

    item = new QStandardItem("realThrdStage");
    model_motionEngine->setItem(10,0,item);
    item = new QStandardItem(QString("%1").arg(info.realThreadStage));
    model_motionEngine->setItem(10,1,item);

    item = new QStandardItem("threadId");
    model_motionEngine->setItem(11,0,item);
    item = new QStandardItem(QString("%1").arg(info.threadId));
    model_motionEngine->setItem(11,1,item);

    item = new QStandardItem("AXISG_stage");
    model_motionEngine->setItem(12,0,item);
    item = new QStandardItem(QString("%1").arg(info.axisGroupInfo.stage));
    model_motionEngine->setItem(12,1,item);

    item = new QStandardItem("AXISG_dTime");
    model_motionEngine->setItem(13,0,item);
    item = new QStandardItem(QString("%1").arg(info.axisGroupInfo.deltaTime));
    model_motionEngine->setItem(13,1,item);

    ui->tableView_motionEngine->setModel(model_motionEngine);
    ui->tableView_motionEngine->resizeColumnToContents(0);
    ui->tableView_motionEngine->resizeColumnToContents(1);
    ui->tableView_motionEngine->resizeColumnToContents(2);
    ui->tableView_motionEngine->show();


}
